package cn.biplam.front.component.view.systemManage.sysprivi.role.manage.privi;

import cn.biplam.common.ajxcode.SystemCode.RoleManageCode;
import cn.biplam.common.ajxcode.SystemCode.RolePriviNaivCode;
import cn.biplam.common.bean.md.IndexTable;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.PriviDeployOptCode;
import cn.biplam.common.opcode.SysManageCode.RolePriviOptCode;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.TreeParam.Index_Tree_Node_Type;
import cn.biplam.common.struc.BaseStruc.IndexTableStruc;
import cn.biplam.common.struc.SysStruc.PriviDeployStruc;
import cn.biplam.common.struc.SysStruc.PriviStruc;
import cn.biplam.common.struc.SysStruc.RolePriviStruc;
import cn.biplam.common.struc.SysStruc.Role_Privi_Flag;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.tree.LinkList;
import cn.biplam.common.wrapper.tree.LinkListNode;
import cn.biplam.common.wrapper.tree.TreeNode;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.data.tree.treebase.TreeCallBackImg;
import cn.biplam.front.component.data.tree.treebase.TreeCallBackNew;
import cn.biplam.front.component.data.tree.treebase.TreeDcCustom;
import cn.biplam.front.component.data.tree.treebase.Tree_Type.Tree_PicType;
import cn.biplam.front.component.data.tree.treebase.img.PlusMinus;
import cn.biplam.front.component.view.tree.TreeBase;
import cn.biplam.front.component.view.tree.TreeView;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.AjxScript.Is_SubMitData;
import cn.biplam.front.template.control.VerticalMenu;

/**
 *  角色权限列表(树结构显示)
 * @author XiaoRuxing Dec 11, 20095:16:43 PM
 * @version 1.0
 */
public class RolePriviList extends TreeView{

	public final static String cId=RolePriviNaivCode.cId;
	/** 树展开动作ID */
	public static final String Expand = RolePriviNaivCode.Expand;
	/** 左键菜单 */
	public static final String Get_Menu = RolePriviNaivCode.Left_Menu;
	
	/** 当前角色 */
	private String roleId;
	
	public RolePriviList() {
		super(Expand);
	}

	/**
	 * 用于Ajx请求时,获取树的页面对象
	 * @param mUser 用户信息
	 * @param sHomeUrl 系统路径
	 * @param params ajx传来的参数集合
	 * @return HtmlWindow 显示树的HtmlWindow
	 * @throws ErrorMessage
	 */
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage{
		if(actionId.equals(Expand)){
			setTree(treeBase,mUser);
			return treeBase.ajxGet(mUser, sHomeUrl, params);
		}else if(actionId.equals(Get_Menu)){
			return this.getLeftMenu();
		}else{
			throw new ErrorMessage("Ajx Exchange outport not found:" + actionId	+ " !");
		}
	}
	
	/**
	 *  设置角色ID
	 * @param roleId
	 */
	public void setRoleId(String roleId){
		this.roleId = roleId;
	}
	
	@Override
	protected void setTree(TreeBase treeBase, UserInfo user)
			throws ErrorMessage {
		//去所有已发布的权限大类
		OperatorResult rs = Operator.get(PriviDeployOptCode.Query_All, 
				mUser.getUserID(), new DataFilter());
		
		if(!TOOLS.exist(rs))return; //无权限大类
		//每个权限类型创建一颗树
		do{
			this.putTree(treeBase, 
						 mUser, 
						 Integer.parseInt(rs.getValue(PriviDeployStruc.SEQ)),
						 rs.getValue(PriviDeployStruc.ROOT_ID),//根ID
						 rs.getValue(PriviDeployStruc.NAME),	//大类名称
						 rs.getValue(PriviDeployStruc.VIRTUAL_ROOT_ID)//虚根ID
						 );
		}while(rs.next());
	}
	
	/**
	 * 使用传入参数,创建一颗权限类型的子树,并放入TreeBase中
	 */
	private void putTree(TreeBase treeBase, 
						 UserInfo mUser,
						 int iTreeIndex,
						 String rootId,
						 String treeVirtualName,
						 String virRootId
				) throws ErrorMessage{
		//设置节点过滤器
		String roleFilterName = ".privi_filter_role";
		if(params!=null){
			this.roleId = params.getByKey(roleFilterName);
		}
		treeBase.setCustomerParam(roleFilterName,this.roleId);
		//获取当前权限树数据
		LinkList priviData = getRolePriviData(roleId, rootId, treeVirtualName,virRootId);
		if(priviData==null)return;
		
		RolePriviCallBack callback = new RolePriviCallBack();
		//设置树DC
		TreeDcCustom curDc = new TreeDcCustom(mUser,treeVirtualName);
		curDc.setData(priviData.getHead());
		curDc.setSetLevelDown("2");
		treeBase.putTreeDc(iTreeIndex, curDc, callback);
		
		/**设置自定义图片*/
		TreeCallBackImg img = TreeCallBackImg.getInstanct(Tree_PicType.Windows);
		img.setPlusMinus(new PlusMinus(false));
		
		treeBase.setPic(iTreeIndex,img);
		
	}

	/**
	 * @return 角色权限树数据
	 * @throws ErrorMessage 
	 */
	private LinkList getRolePriviData(String roleId, String rootPriviId,
			String rootPriviName,String virRootId) throws ErrorMessage{
		LinkList list = null;
		//获取当前角色对应的所有权限数据
		OperatorResult priviChild = getRolePriviChild(roleId, rootPriviId);
		if(TOOLS.exist(priviChild)){
			list = new LinkList();
			TreeNode rootNode=new TreeNode(virRootId,null,null);
			rootNode.setIsRoot("0");
			LinkListNode<TreeNode> root=new LinkListNode<TreeNode>(rootNode);
			list.setHead(root);
			//创建根节点
			TreeNode head = new TreeNode(rootPriviId,virRootId,rootPriviName);
			head.setOperator(Index_Tree_Node_Type.ROOT_THEME.toString());
			head.setSequence("1");
			list.insert(head);
			//创建下级节点
			createPriviNode(priviChild, list, roleId);
		}
		return list;
	}
	
	/**
	 *  创建指定用户权限节点及其下级节点
	 */
	private void createPriviNode(OperatorResult childs,LinkList list,String roleId) throws ErrorMessage{
		if(TOOLS.exist(childs)){
			do{
				//创建当前节点
				TreeNode node = new TreeNode(childs.getValue(PriviStruc.PRIVI_ID),
						childs.getValue(PriviStruc.PRIVI_PARENT),
						childs.getValue(PriviStruc.PRIVI_NAME));
				node.setSequence(childs.getValue(IndexTableStruc.NODE_SEQUENCE));
				node.setOperator(childs.getValue(IndexTableStruc.OPERATOR_TYPE));
				node.setNodeType(childs.getValue(IndexTableStruc.NODE_TYPE));
				list.insert(node);
				//创建当前节点下级节点
				createPriviNode(getRolePriviChild(roleId, childs.getValue(PriviStruc.PRIVI_ID)),
						list,roleId);
			}while(childs.next());
		}
	}
	
	/**
	 * @return 执行角色和权限下的下级角色权限
	 * @throws ErrorMessage
	 */
	private OperatorResult getRolePriviChild(String roleId,String priviId) throws ErrorMessage{
		DataFilter filter = new DataFilter(priviId);
		filter.put(RolePriviStruc.ROLE_ID,roleId);
		OperatorResult result = Operator.get(RolePriviOptCode.Query_Privi_Child, mUser.getUserID(), filter);
		return result;
	}
	
	/**
	 * @return 获取点击后操作按钮
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getLeftMenu() throws ErrorMessage{
		VerticalMenu menu = new VerticalMenu(cId,recordId);
		menu.put(CaptionComp.get(mUser.getUserID(), SysCaption.Delete), 
				AjxScript.getPr(menu.getID(), RoleManageCode.Role_Privi_Del, Is_SubMitData.NoSubmit)
				);
		return menu.get();
	}
	
	/**
	 * 权限角色列表树内容回调器
	 */
	class RolePriviCallBack extends TreeCallBackNew{

		public RolePriviCallBack() throws ErrorMessage {
			super();
		}
		
		/**
		 * 取树显示的文本字符串
		 * @param nodeId 节点id
		 * @param operatorType 节点类型
		 * @param pageId 页面id，这里用树的pcid+actionId
		 * @return String 树显示的文本字符串
		 * @throws ErrorMessage 
		 */
		public HtmlWindow getText(String nodeId, String operatorType,String pageId,String roleId) throws ErrorMessage {
			String containerId = nodeId+".list";
			
			//获取当前权限信息
			IndexTable index = IndexTable.getNewInstance(mUser.getUserID(), PriviStruc.Table_Name,nodeId);
			String priviName = index.getNodeName();
			String priviDesc = index.getNodeDesc();
			String priviParent = index.getNodeParentId();
			//获取当前角色权限信息
			DataFilter filter = new DataFilter();
			filter.put(RolePriviStruc.ROLE_ID,roleId);
			filter.put(RolePriviStruc.PRIVI_ID,nodeId);
			OperatorResult curRolePriv = 
				Operator.get(RolePriviOptCode.Query_By_PriviAndRole_ID, mUser.getUserID(), filter);
			String rolePriviId = curRolePriv.getValue(RolePriviStruc.ID);//获取当前角色权限ID
			String rolePriviType = curRolePriv.getValue(RolePriviStruc.SUPPER_FLAG);
			
			//获取当前角色和父权限信息
			DataFilter aFilter = new DataFilter();
			aFilter.put(RolePriviStruc.ROLE_ID,roleId);
			aFilter.put(RolePriviStruc.PRIVI_ID,priviParent);
			String roleParentPriviType = "";
			OperatorResult parentRolePriv = 
				Operator.get(RolePriviOptCode.Query_By_PriviAndRole_ID, mUser.getUserID(), aFilter);
			if(TOOLS.exist(parentRolePriv)){
				roleParentPriviType = parentRolePriv.getValue(RolePriviStruc.SUPPER_FLAG);
			}
			
			//当前节点
			HtmlDiv div = new HtmlDiv(containerId);
			div.put(new HtmlValue(containerId+"r",rolePriviId));
			HtmlWindow name = new HtmlText(null,null,priviName);
			name.setMarginTop("1");
			div.put(name);
			
			// 如果角色权限标志不是完全拥有,禁止删除操作 
			// 如果角色拥有当前权限父权限的完全权限在删除父权限前不能删除当前权限
			if(!rolePriviType.equals(Role_Privi_Flag.SUPPER.toString())
					|| roleParentPriviType.equals(Role_Privi_Flag.SUPPER.toString())){
				
			}else{
				div.bind(Window_Event.OnClick,
						AjxScript.getPm(containerId, RolePriviNaivCode.Left_Menu,null));
			}
			
			//计算当前节点宽度
			int tableCellWidth = 14;
			int length = TOOLS.getTextLength(priviName)+tableCellWidth+    //名称内容宽度
							TOOLS.getTextLength(priviDesc)+tableCellWidth+  //描述内容宽度
							45;//按钮宽度
			div.setStyleWidth(length);
			HtmlWindow win = div;
			win.setFloat(Float_Type.Left);
			win.setLength(length);
			return win;
		}
	}
}
